export default {
    data() {
        return {
            student_id: 0,
            video_id:0,
            ruleForm: {
                postData: {},
                band: [],
                video:'',
                music_name:'',
                music_desc:''
            },
            rules: {
                music_name: [
                    {required: true, message: '请输入曲目名称', trigger: 'blur'},
                ],
                music_desc: [
                    {required: true, message: '请输入曲目简介', trigger: 'blur'},
                ],
                video: [
                    {required: true, message: '请上传视频', trigger: 'blur'},
                ],
            },
            postData: {},			//七牛云postData
            domain: '',				//图片域名
            is_upload_video:false,
            preview_video_dialog:false,
        }
    },

    //进入页面加载
    mounted: function () {
        var that = this;
        //在缓存中获取值
        that.user_name = sessionStorage.getItem("user_name");
        that.user_id = sessionStorage.getItem("user_id");
        that.token = sessionStorage.getItem("access-token");
        if ((!that.token) || (!that.user_id) || (!that.user_name)) {
            that.$router.push('/login');
        }

        //加载轮播详情
        if (that.$route.query.student_id) {
            that.student_id = that.$route.query.student_id;
            that.detail();
        }

        //获取七牛云token
        that.getQiNiuToken();
    },

    //方法
    methods: {
        //请求api
        submitForm(formName) {
            var that = this;
            console.log(that.video_id);
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    if (that.video_id === 0) {
                        that.add();
                    } else {
                        that.edit();
                    }
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },

        //获取七牛云token
        getQiNiuToken: function () {
            var that = this;
            //请求登陆接口
            that.$http.post(that.adminApi.api_url + "/Qiniu/getToken", {
                token: that.token,
            }, {
                emulateJSON: true
            }).then(
                function (res) {
                    // 处理成功的结果
                    that.postData = {
                        token: res.body.upToken,
                    }
                    that.domain = res.body.domain;
                },
                function () {
                    // 处理失败的结果
                    that.$message({
                        type: 'error',
                        message: `操作提示: ${ '处理异常' }`
                    });
                });
        },

        /**
         * 预览视频
         */
        preview_video: function (e) {
            let that = this;
            that.preview_video_dialog = true;
        },

        /**
         * 上传视频-成功
         * @param e
         */
        upload_video_success: function (e) {
            let that = this;
            that.ruleForm.video = that.domain + e.key;
            that.is_upload_video = true;
        },

        /**
         * 移除视频
         */
        remove_video: function (e) {
            let that = this;
            that.ruleForm.video = '';
            that.is_upload_video = false;
        },


        /**
         * 移除视频前的操作
         */
        before_remove_video: function (e) {
            let that = this;

        },


        handle_exceed_video: function (e) {
            let that = this;
            console.log('预览视频4', e);
        },


        /**
         * 详情
         */
        detail: function () {
            var that = this;
            //请求的数据
            var formData = {};
            formData.token = that.token;
            formData.id = that.student_id;

            //请求邀请者列表
            that.$http.post(that.adminApi.api_url + "/User_Identity/show_edit", formData, {
                emulateJSON: true
            }).then(
                function (res) {
                    if(res.body.video_info.length !== 0) {
                        that.video_id = res.body.video_info.id;
                        that.ruleForm.music_name = res.body.video_info.title;
                        that.ruleForm.music_desc = res.body.video_info.describe;
                        if(res.body.video_info.video_file){
                            that.ruleForm.video = res.body.video_info.video_file;
                            that.is_upload_video = true;
                        }
                    }
                });
        },

        /**
         * 添加
         * @returns {boolean}
         */
        add: function () {
            var that = this;
            //请求的数据
            var formData = {};
            formData.token = that.token;
            formData.user_id = that.user_id;
            formData.user_identity_id = that.student_id;
            formData.title = that.ruleForm.music_name;
            formData.video_file = that.ruleForm.video;
            formData.describe = that.ruleForm.music_desc;

            //请求API
            that.$http.post(that.adminApi.api_url + "/User_Video/add", formData, {
                emulateJSON: true
            }).then(
                function (res) {
                    that.$router.push({path: '/seller/student_bind',query:{student_id:that.student_id}});
                },
                function () {
                    // 处理失败的结果
                    that.$message({
                        type: 'error',
                        message: `操作提示: ${ '处理异常' }`
                    });
                });
        },

        /**
         * 修改
         * @returns {boolean}
         */
        edit: function () {
            var that = this;
            //请求的数据
            var formData = {};
            formData.token = that.token;
            formData.user_id = that.user_id;
            formData.user_identity_id = that.student_id;
            formData.title = that.ruleForm.music_name;
            formData.video_file = that.ruleForm.video;
            formData.describe = that.ruleForm.music_desc;
            formData.id = that.video_id;

            //请求API
            that.$http.post(that.adminApi.api_url + "/User_Video/edit", formData, {
                emulateJSON: true
            }).then(
                function (res) {
                    that.$router.push({path: '/seller/student_bind',query:{student_id:that.student_id}});
                },
                function () {
                    // 处理失败的结果
                    that.$message({
                        type: 'error',
                        message: `操作提示: ${ '处理异常' }`
                    });
                });
        },

        //返回
        cancel: function () {
            var that = this;
            that.$router.push({path: '/seller/student'});
        },

        //加载所有品牌
        allBrand: function () {
            var that = this;
            //请求的数据
            var formData = {};
            formData.token = that.token;

            //请求邀请者列表
            that.$http.post(that.adminApi.api_url + "/api/brands/brandlist", formData, {
                emulateJSON: true
            }).then(
                function (res) {
                    for (var i in res.body) {
                        that.brandList.push({
                            'value': res.body[i].id,
                            'label': res.body[i].english_name
                        })
                    }
                },
                function () {
                    // 处理失败的结果
                    that.$message({
                        type: 'error',
                        message: `操作提示: ${ '处理异常' }`
                    });
                });
        },
    }
}